﻿using System;
using System.Collections.Generic;
using System.Text;
using DevComponents.AdvTree;
using AMS.UI.Controls.Search;
using System.Windows.Forms;
using AMS.UI.Controls;

namespace AMS.Trademark.Management.Helpers
{
    /// <summary>
    /// 带搜索功能的树
    /// </summary>
    public class SearchingAdvTree
    {
        public SearchingAdvTree(AdvTree tree, ISearchTool searchTool)
        {
            Tree = tree;
            SearchTool = searchTool;
            Tree.KeyDown += new System.Windows.Forms.KeyEventHandler(Tree_KeyDown);
            SearchTool.KeywordChanged += new EventHandler<KeywordChangedEventArgs>(SearchTool_KeywordChanged);
        }

        public AdvTree Tree { get; private set; }
        public ISearchTool SearchTool { get; private set; }

        void Tree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.F)
            {
                SearchTool.ShowTo(Tree);
            }
            else if (!e.Control && e.KeyCode == Keys.F3)
            {
                SearchTool.ShowTo(Tree);
            }
        }

        void SearchTool_KeywordChanged(object sender, KeywordChangedEventArgs e)
        {
            DoSearch(e.Keyword, e.First);
        }

        private bool DoSearch(string text, bool isFirst)
        {
            AdvTree tv = Tree;

            if (tv.Nodes.Count == 0 || string.IsNullOrEmpty(text))
                return false;

            Node startNode = null;
            if (tv.SelectedNode != null)
            {
                startNode = GetNextNode(tv.SelectedNode, isFirst);
            }

            if (startNode == null)
            {
                startNode = tv.Nodes[0];
            }

            Node node = startNode;
            bool bContinue = true;
            bool bFound = false;
            while (bContinue)
            {
                if (0 <= node.Text.IndexOf(text, StringComparison.CurrentCultureIgnoreCase))
                {
                    bContinue = false;
                    if (node.Parent != null)
                    {
                        Node pTmpNode = node.Parent;
                        do
                        {
                            pTmpNode.Expand();
                            pTmpNode = pTmpNode.Parent;
                        } while (pTmpNode != null);
                    }

                    tv.SelectedNode = node;
                    bFound = true;
                    break;
                }
                else
                {
                    node = GetNextNode(node, false);
                    if (node == null)
                        node = tv.Nodes[0];

                    if (node == startNode)
                        bContinue = false;
                }
            }

            return bFound;
        }

        private Node GetNextNode(Node node, bool isFirst)
        {
            if (isFirst)
            {
                return node;
            }

            if (node.Nodes.Count > 0)
                return node.Nodes[0];

            Node nextNode = node.NextNode;
            Node parentNode;
            if (nextNode == null)
            {
                parentNode = node.Parent;
                do
                {
                    if (parentNode != null)
                    {
                        nextNode = parentNode.NextNode;
                        if (nextNode == null)
                            parentNode = parentNode.Parent;
                        else
                            break;
                    }
                } while (parentNode != null);
            }

            return nextNode;
        }
    }
}
